package club.kingyin.kyblogprosearch.controller;

import club.kingyin.commons.clients.FileClient;
import club.kingyin.commons.clients.UserClient;
import club.kingyin.commons.constant.AuthorizeConstant;
import club.kingyin.commons.utils.Commons;
import club.kingyin.easycache.annotation.EasyCache;
import club.kingyin.easycache.core.impl.AbstractEasyCacheKey;
import club.kingyin.easycache.core.impl.DefaultEasyCache;
import club.kingyin.kyblogprosearch.core.NetWorldCloud;
import club.kingyin.kyblogprosearch.service.DocumentService;

import club.kingyin.commons.entity.core.Document;
import club.kingyin.commons.response.Result;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.Background;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.bg.PixelBoundaryBackground;
import com.kennycason.kumo.collide.RectanglePixelCollidable;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizer.api.WordTokenizer;
import com.kennycason.kumo.palette.ColorPalette;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import javax.annotation.Resource;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;

@RestController
@RequestMapping("/search")
public class DocumentController {

    @Resource
    private DocumentService documentService;

    @Resource
    private DefaultEasyCache defaultEasyCache;

    @GetMapping("/index/exists")
    public Result existsIndexDocument() {
        return Result.success(documentService.hasDocument() ? "索引已存在" : "未找到索引");
    }


    @PostMapping("/save")
    public Result saveDocument(@RequestBody Document document) {
        co.elastic.clients.elasticsearch._types.Result save = documentService.save(document);
        if (save != null) {
            defaultEasyCache.remove(new AbstractEasyCacheKey() {
                @Override
                protected void setPrams() {

                }
                @Override
                public boolean equals(String key) {
                    return true;
                }
            },"ky-search","searchAllTitle","searchTitle","searchAll","searchByTag","searchByClassify");
        }
        return Result.success(save);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Document document){
        co.elastic.clients.elasticsearch._types.Result update = documentService.update(document);
        if (update != null) {
            defaultEasyCache.remove(new AbstractEasyCacheKey() {
                @Override
                protected void setPrams() {

                }
                @Override
                public boolean equals(String key) {
                    return true;
                }
            },"ky-search","searchAllTitle","searchTitle","searchAll","searchByTag","searchByClassify");
        }
        return Result.success(update);
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam("id") String id){
        try {
            co.elastic.clients.elasticsearch._types.Result delete = documentService.delete(id);
            if (delete != null) {
                defaultEasyCache.remove(new AbstractEasyCacheKey() {
                    @Override
                    protected void setPrams() {

                    }
                    @Override
                    public boolean equals(String key) {
                        return true;
                    }
                },"ky-search","searchAllTitle","searchTitle","searchAll","searchByTag","searchByClassify");
            }
            return Result.success(delete);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.failN("操作失败");
    }

    @EasyCache(prams = {"title"}, module = "ky-search")
    @GetMapping("/title/all")
    public Result searchAllTitle(@RequestParam("title") String title) {
        long currentTimeMillis = System.currentTimeMillis();
        List<Document> documents = documentService.searchAllTitle(title);
        System.out.println("标题搜索耗时："+(System.currentTimeMillis()-currentTimeMillis));
        return Result.success(documents);
    }

    @EasyCache(prams = {"userid","title"}, module = "ky-search")
    @GetMapping("/title")
    public Result searchTitle(@RequestHeader(AuthorizeConstant.ID) int userid, @RequestParam("title") String title){
        return Result.success(documentService.searchUserTitle(title,userid));
    }

    @EasyCache(prams = {"context","current","size"}, module = "ky-search")
    @GetMapping("/all")
    public Result searchAll(@RequestParam("context") String context, @RequestParam("current") int current, @RequestParam("size") int size) {
        return Result.success(documentService.searchAll(context,(current-1)*size,size));
    }

    @EasyCache(prams = {"tag","current","size"}, module = "ky-search")
    @GetMapping("/tag")
    public Result searchByTag(@RequestParam("tag") String tag, @RequestParam("current") int current, @RequestParam("size") int size) {
        return Result.success(documentService.searchTag(tag,(current-1)*size,size));
    }

    @EasyCache(prams = {"classify","current","size"}, module = "ky-search")
    @GetMapping("/classify")
    public Result searchByClassify(@RequestParam("classify") String classify, @RequestParam("current") int current, @RequestParam("size") int size) {
        return Result.success(documentService.searchClassify(classify,(current-1)*size,size));
    }

    @PostMapping("/recommend")
    public Result recommend(@RequestBody Document document) {
        return Result.success(documentService.recommend(document));
    }

    @GetMapping("/analyzer")
    public Result analyzer(@RequestParam("title") String title) {
        return Result.success(documentService.analyzer(title));
    }

    @SneakyThrows
    @GetMapping("/recommend/home")
    public Result recommendHome(@RequestHeader(AuthorizeConstant.ID) int id) {
        long currentTimeMillis = System.currentTimeMillis();
        ArrayList<String> filter = new ArrayList<>();
        // 1.热点推送
        List<Document> hots = documentService.hotDocument(filter);
        System.out.println("热点耗时："+(System.currentTimeMillis()-currentTimeMillis));
        // 2.个性化推荐
        currentTimeMillis = System.currentTimeMillis();
        List<Document> recommend = documentService.recommend(id, filter);
        System.out.println("推荐耗时："+(System.currentTimeMillis()-currentTimeMillis));
        // 3.最多阅读推荐
        currentTimeMillis = System.currentTimeMillis();
        List<Document> moreViews = documentService.moreViews(filter);
        System.out.println("最多耗时："+(System.currentTimeMillis()-currentTimeMillis));

        // 4.最近发布推荐
        currentTimeMillis = System.currentTimeMillis();
        List<Document> lastTimestamp = documentService.lastTimestamp(filter);
        System.out.println("最近耗时："+(System.currentTimeMillis()-currentTimeMillis));

        // 5.随机推荐
        currentTimeMillis = System.currentTimeMillis();
        List<Document> random = documentService.random(4, filter);
        System.out.println("随机耗时："+(System.currentTimeMillis()-currentTimeMillis));

        HashMap<String, List<Document>> home = new HashMap<>();
        home.put("hot",hots);
        home.put("recommend",recommend);
        home.put("moreView",moreViews);
        home.put("lastTimestamp",lastTimestamp);
        home.put("random",random);

        return Result.success(home);
    }
}
